#!/bin/sh

# Convenient script to create commit for update of submodules.
#
# NOTE: This script can only handle one submodule update. In the spirit of
# keeping commits atomic, we typically shouldn't update multiple submodules in
# the same commit anyway.
#
# Author: Zeeshan Ali <zeeshan.ali@pelagicore.com>
#         Gunnar Andersson <gandersson@genivi.org>

TMPFILE=/tmp/commit.$$
COMMIT_SUMMARY="$*"

# The commit log almost always needs to be edited, so let's make also the 
# summary line optional. If not given, add a clear indication that it needs to
# be written in the editor
if test -z "${COMMIT_SUMMARY}"; then
    COMMIT_SUMMARY="*** PUT SUMMARY HERE ***"
fi

# If multiple submodules are changed, we take the first one so that each one is
# handled separately.
CHANGED_MOD=$(git submodule status | grep '^+' | tr -d + | head -n 1)
if test -z "${CHANGED_MOD}"; then
    echo "No module updated."
    exit 2
fi

SUBMODULE=$(echo "${CHANGED_MOD}" | cut -d' ' -f2)
NEWCOMMIT=$(echo "${CHANGED_MOD}" | cut -d' ' -f1)
OLDCOMMIT=$(git diff "${SUBMODULE}" | grep "^-Subproject" | cut -d' ' -f3)

cat <<EOT >"${TMPFILE}"
${SUBMODULE}: ${COMMIT_SUMMARY}

$(git -C ${SUBMODULE} log --no-merges --format="%h %s" ${OLDCOMMIT}..${NEWCOMMIT})
EOT

git commit -s -e --file "${TMPFILE}" "${SUBMODULE}"
